/*
 * @(#)AdlText.java   09/09/13
 *
 * Copyright (C) 2009 Ecole des Mines d'Ales
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Contact: yulinz88@gmail.com
 *
 *
 *
 */



package fr.ema.arch3d.gea.config;

import fr.ema.arch3d.model.architecture.configuration.ComponentClass;
import fr.ema.arch3d.model.architecture.configuration.Configuration;
import fr.ema.arch3d.model.architecture.configuration.ConnectorClass;

import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

/**
 * Class description
 *
 *
 * @version        1.0, 09/09/13
 * @author         Huaxi (Yulin) ZHANG
 */
public class AdlText {

    /** Field description */
    private JTextPane textPane;

    /**
     * Constructs ...
     *
     */
    public AdlText() {
        textPane = new JTextPane();

        // textPane.setBackground(Color.black);
        textPane.setEditable(false);
    }

    public void setBold(String str) {
        SimpleAttributeSet attrset = new SimpleAttributeSet();

        /*
         * StyleConstants.setForeground(attrset,Color.black);
         * StyleConstants.setUnderline(attrset,false);
         * StyleConstants.setItalic(attrset,false);
         */
        StyleConstants.setBold(attrset, true);

        // StyleConstants.setFontSize(attrset,22);
        insert(str, attrset);
    }

    public void insert(String str, AttributeSet attrset) {
        Document docs = textPane.getDocument();

        str = str;

        try {
            docs.insertString(docs.getLength(), str, attrset);
        } catch (BadLocationException ble) {
            System.out.println("BadLocationException:" + ble);
        }
    }

    public void insert(String str) {
        Document docs = textPane.getDocument();

        str = str;

        try {
            docs.insertString(docs.getLength(), str, null);
        } catch (BadLocationException ble) {
            System.out.println("BadLocationException:" + ble);
        }
    }

    public void changeLine() {
        Document docs = textPane.getDocument();
        String   str  = "\n";

        try {
            docs.insertString(docs.getLength(), str, null);
        } catch (BadLocationException ble) {
            System.out.println("BadLocationException:" + ble);
        }
    }

    public void addSpace() {
        Document docs = textPane.getDocument();
        String   str  = "  ";

        try {
            docs.insertString(docs.getLength(), str, null);
        } catch (BadLocationException ble) {
            System.out.println("BadLocationException:" + ble);
        }
    }

    public java.awt.Component getComponent() {
        return textPane;
    }

    public Document createTextConfig(Configuration config) {
        AdlText pane = new AdlText();

        // describe configuration name
        pane.setBold("configuration");
        pane.addSpace();
        pane.insert(config.name);
        pane.changeLine();
        pane.addSpace();
        pane.setBold("implements");

        // describe specification which this configuration implements
        pane.addSpace();
        pane.insert(config.specImpl.name);
        pane.changeLine();

        // describe components
        pane.addSpace();
        pane.setBold("component_classes");
        pane.changeLine();

        for (int i = 0; i < config.cmp_impl_list.size(); i++) {
            pane.addSpace();
            pane.addSpace();

            String a[] = (String[]) config.cmp_impl_list.get(i);

            pane.insert(a[0]);
            pane.addSpace();
            pane.setBold("as");
            pane.addSpace();
            pane.insert(a[1] + ";");

            // pane.setBold("implements");
            // pane.addSpace();
            // pane.insert(a[1]+";");
            pane.changeLine();
        }

        // describe connectors
        pane.addSpace();
        pane.addSpace();
        pane.setBold("connector_classes");
        pane.changeLine();

        for (int i = 0; i < config.cnt_impl_list.size(); i++) {
            pane.addSpace();
            pane.addSpace();

            String a[] = (String[]) config.cnt_impl_list.get(i);

            pane.insert(a[0]);
            pane.addSpace();
            pane.setBold("as");
            pane.addSpace();
            pane.insert(a[1] + ";");
            pane.changeLine();
        }

        // end describe configuration
        pane.setBold("end");
        pane.addSpace();
        pane.insert(config.name);
        pane.changeLine();

        Document docs = ((JTextPane) pane.getComponent()).getDocument();

        // System.out.println("hhhhhhhhhh");
        return docs;
    }

    public Document createTextCmp(ComponentClass cmp) {
        AdlText pane = new AdlText();

        // describe component ytpe
        pane.setBold("component");
        pane.addSpace();
        pane.insert(cmp.name);
        pane.changeLine();

        // describe implement component type
        pane.addSpace();
        pane.addSpace();
        pane.setBold("implement");
        pane.addSpace();
        pane.insert(cmp.crImpl.name);
        pane.changeLine();

        // describe component implementation class
        pane.addSpace();
        pane.addSpace();
        pane.setBold("with_class");
        pane.addSpace();
        pane.insert(cmp.implClass);
        pane.changeLine();

        // describe version information
        pane.addSpace();
        pane.addSpace();
        pane.setBold("version");
        pane.addSpace();
        pane.insert(cmp.version.reversion_numb);
        pane.changeLine();

        // end description
        pane.setBold("end");
        pane.addSpace();
        pane.insert(cmp.name);

        Document docs = ((JTextPane) pane.getComponent()).getDocument();

        return docs;
    }

    /**
     *
     * @param cnt: the connection which need to describe
     * @return the document which describe the connection using ADL grammar
     */
    public Document createTextCnt(ConnectorClass ctr) {
        AdlText pane = new AdlText();

        // describe connector
        pane.setBold("connector");
        pane.addSpace();
        pane.insert(ctr.name);
        pane.changeLine();

        // describe implement connection
        pane.addSpace();
        pane.addSpace();
        pane.setBold("implement");
        pane.addSpace();
        pane.insert(ctr.connection.name);
        pane.changeLine();

        // describe connector implementation class
        pane.addSpace();
        pane.addSpace();
        pane.setBold("with_class");
        pane.addSpace();
        pane.insert(ctr.connectorImpl);
        pane.changeLine();

        // end description
        pane.setBold("end");
        pane.addSpace();
        pane.insert(ctr.name);

        Document docs = ((JTextPane) pane.getComponent()).getDocument();

        return docs;
    }
}
